/*
 * Copyright 2017, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ctyon.watchmanager.ui.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import com.ctyon.watchmanager.R;
import com.ctyon.watchmanager.bean.ProgramBean;
import com.ctyon.watchmanager.databinding.ItemProgramBinding;
import com.ctyon.watchmanager.listener.ItemClickListener;
import com.ctyon.watchmanager.listener.ProgramItemClickListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

public class ProgramAdapter extends RecyclerView.Adapter<ProgramAdapter.ProgramViewHolder> {

    List<ProgramBean> mProgramBeanList = new ArrayList<>();

    @Nullable
    private final ProgramItemClickListener itemClickListener;

    public ProgramAdapter(@Nullable ProgramItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
        setHasStableIds(true);
    }

    public void setData(final List<ProgramBean> programs) {
        if (mProgramBeanList.size() > 0){
            mProgramBeanList.clear();
        }
        mProgramBeanList.addAll(programs);
        notifyDataSetChanged();
    }

    public void add(ProgramBean program){
        mProgramBeanList.add(program);
        notifyItemInserted(mProgramBeanList.size()-1);
    }

    public void remove(int position){
        mProgramBeanList.remove(position);
        notifyDataSetChanged();
    }

    public List<ProgramBean> getPrograms(){
        return mProgramBeanList;
    }

    @Override
    public ProgramViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.d("Lee_ProgramAdapter", "onCreateViewHolder");
        ItemProgramBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_program, parent, false);
        return new ProgramViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(ProgramViewHolder holder, int position) {
        Log.d("Lee_ProgramAdapter", "onBindViewHolder");
        holder.binding.setProgram(mProgramBeanList.get(position));
        holder.binding.setIndex(position);
        holder.binding.programEnable.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (buttonView.isPressed()){
                itemClickListener.onEnableClick(position, isChecked);
            }
        });
        holder.binding.executePendingBindings();
    }

    @Override
    public int getItemCount() {
        Log.d("Lee_ProgramAdapter", "getItemCount");
        return mProgramBeanList == null ? 0 : mProgramBeanList.size();
    }

    @Override
    public long getItemId(int position) {
        Log.d("Lee_ProgramAdapter", "getItemId position = " + position);
        return position;
    }

    static class ProgramViewHolder extends RecyclerView.ViewHolder {

        final ItemProgramBinding binding;

        public ProgramViewHolder(ItemProgramBinding binding) {
            super(binding.getRoot());
            Log.d("Lee_ProgramAdapter", "ChatsViewHolder");
            this.binding = binding;
        }
    }

}
